//
//  GAONetworkTools.swift
//  Weibo28
//
//  Created by 高辰 on 16/5/12.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestType: Int {
    //GET
    case GET = 0
    //POST
    case POST = 1
}

class GAONetworkTools: AFHTTPSessionManager {
    static let NetworkTools: GAONetworkTools = {
        let tools = GAONetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    //  网络请求封装 get/ post
    func request(type: RequestType, url: String, parameters: AnyObject?, callBack: (response: AnyObject?, error:NSError?)->()) {
        
        if type == RequestType.GET {
            GET(url, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                
                callBack(response: response, error: nil)
                
                }, failure: { (_, error) -> Void in
                    
                    callBack(response: nil, error: error)
            })
            
        }else {
            POST(url, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                
                callBack(response: response, error: nil)
                
                }, failure: { (_, error) -> Void in
                    
                    callBack(response: nil, error: error)
                    
            })
        }
        
    }
    
    //封装上传请求
    func requestUpload(url: String, parameters: AnyObject?, data: NSData, name: String, callBack: (response: AnyObject?, error:NSError?)->()) {
        
        POST(url, parameters: parameters, constructingBodyWithBlock: { (formData) -> Void in
            
            //  For a list of valid MIME types, see http://www.iana.org/assignments/media-types/
            
            //  data 图片对应二进制数据
            //  name 服务端需要的参数
            //  filename 服务端获取你传入图片的名字,一般服务端不会使用,它们自己会随机生成一个唯一标记的图片名
            //  mimeType 资源类型 application/octet-stream 通用类型
            formData.appendPartWithFileData(data, name: "pic", fileName: "test", mimeType: "application/octet-stream")
            
            }, progress: nil, success: { (_, response) -> Void in
                callBack(response: response, error: nil)
            }) { (_, error) -> Void in
                callBack(response: nil, error: error)
        }
    }
    
}



//  OAuth相关接口
extension GAONetworkTools {
    //  获取accesstoken的接口
    func requestAccesstoken(code: String, callBack: (response: AnyObject?, error:NSError?)->()) {
        //  创建url
        let url = "https://api.weibo.com/oauth2/access_token"
        
        let parameters = [
            "client_id": client_id,
            "client_secret": AppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": redirect_uri
        ]
        
        request(RequestType.POST, url: url, parameters: parameters, callBack: callBack)
    }
    
    //  获取用户信息
    func requestUserInfo(userAccount: GAOUserAccount, callBack: (response: AnyObject?, error:NSError?)->()){
        let url = "https://api.weibo.com/2/users/show.json"
        
        let parameters = [
            "access_token": userAccount.access_token!,
            "uid": "\(userAccount.uid)"
        ]
        
        request(RequestType.GET, url: url, parameters: parameters, callBack: callBack)
    }
}

//  首页相关接口
extension GAONetworkTools {
    /**
     请求微博首页数据
     获取当前登录用户及其所关注（授权）用户的最新微博
     */
    func requestStatuses(accessToken: String, maxId: Int64, sinceId: Int64, callBack: (response: AnyObject?, error:NSError?)->()) {
        // 创建url
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        // 创建参数
        let parameters = [
            "access_token": accessToken,
            "max_id": "\(maxId)",
            "since_id": "\(sinceId)"
        ]
        request(RequestType.GET, url: url, parameters: parameters, callBack: callBack)
        
    }
    
}

//发微博相关接口
extension GAONetworkTools {
    
    //发布图片微博接口
    func requsetUpload(access_token: String, status: String, image: UIImage,  callBack: (response: AnyObject?, error:NSError?)->()) {
        // 创建url
        let url = "https://upload.api.weibo.com/2/statuses/upload.json"
        // 创建参数
        let parameters = [
            "access_token": access_token,
            "status": status
        ]
        //将图片转换为二进制
        let data = UIImageJPEGRepresentation(image, 0.6)!
        
        requestUpload(url, parameters: parameters, data: data, name: "pic", callBack: callBack)
        
    }
    
    //发送文字微博接口
    func requsetUpdate(access_token: String, status: String, callBack: (response: AnyObject?, error:NSError?)->()) {
        // 创建url
        let url = "https://api.weibo.com/2/statuses/update.json"
        // 创建参数
        let parameters = [
            "access_token": access_token,
            "status": status
        ]
        request(RequestType.POST, url: url, parameters: parameters, callBack: callBack)
    }
}



